package com.wms.shop.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms.common.core.utils.DateUtils;
import com.wms.shop.domain.Address;
import com.wms.shop.mapper.AddressMapper;
import com.wms.shop.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wms.common.core.utils.SecurityUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 客户地址Service业务层处理
 *
 * @author zzm
 * @date 2021-12-19
 */
@Service
public class AddressServiceImpl implements IAddressService
{
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询客户地址
     *
     * @param id 客户地址ID
     * @return 客户地址
     */
    @Override
    public Address selectAddressById(Long id)
    {
        return addressMapper.selectById(id);
    }

    /**
     * 查询客户地址列表
     *
     * @param address 客户地址
     * @return 客户地址
     */
    @Override
    public List<Address> selectAddressList(Address address)
    {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Address::getUserId, address.getUserId());
        return addressMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户地址
     *
     * @param address 客户地址
     * @return 结果
     */
    @Override
    public int insertAddress(Address address)
    {
        address.setDelFlag(false);
        address.setCreateBy(SecurityUtils.getUsername());
        address.setCreateTime(DateUtils.getNowDate());
        return addressMapper.insert(address);
    }

    /**
     * 修改客户地址
     *
     * @param address 客户地址
     * @return 结果
     */
    @Override
    public int updateAddress(Address address)
    {
        address.setUpdateBy(SecurityUtils.getUsername());
        address.setUpdateTime(DateUtils.getNowDate());
        return addressMapper.updateById(address);
    }

    /**
     * 批量删除客户地址
     *
     * @param ids 需要删除的客户地址ID
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(Long[] ids)
    {
        Address address = new Address();
        address.setDelFlag(Boolean.TRUE);
        address.setUpdateTime(DateUtils.getNowDate());
        address.setUpdateBy(SecurityUtils.getUsername());
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",Arrays.asList(ids));
        return addressMapper.update(address, queryWrapper);
    }

    /**
     * 删除客户地址信息
     *
     * @param id 客户地址ID
     * @return 结果
     */
    @Override
    public int deleteAddressById(Long id)
    {
        return addressMapper.deleteById(id);
    }
}